<!--
 * @Author: your name
 * @Date: 2021-07-08 22:06:01
 * @LastEditTime: 2021-07-09 18:01:48
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \day1\vue_shop\src\components\login.vue
-->
<!--  -->
<template>
  <div class="com-container" ref="infoBox">
    <div class="com-chart">
      <div class="login-box">
        <!-- 头像区域 -->
        <div class="tou_box">
          <img src="~assets/img/ningweitou.jpg" alt="" />
        </div>
        <!-- 登录表单区域 -->
        <el-form
          class="login_form"
          label-width="0px"
          :model="LoginForm"
          :rules="rule"
          ref="loginFormref"
        >
          <!-- 用户名 -->
          <el-form-item prop="username">
            <el-input
              prefix-icon="iconfont icon-user"
              v-model="LoginForm.username"
            ></el-input>
          </el-form-item>
          <!-- 密码-->
          <el-form-item prop="password">
            <el-input
              prefix-icon="iconfont icon-3702mima"
              v-model="LoginForm.password"
              type="passWord"
            ></el-input>
          </el-form-item>
          <!-- 按钮区域 -->
          <el-form-item class="btns">
            <el-button type="primary" round @click="loginForm">登录</el-button>
            <el-button type="info" round @click="resetloginForm"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //表单的数据绑定
      LoginForm: {
        username: 'admin',
        password: '1234567'
      },
      // 这是表单的验证规则对象
      rule: {
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'change' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    //this.a()
  },
  methods: {
    //   a(){
    //       let height = this.$refs.infoBox.clientHeight;
    //       let width = this.$refs.infoBox.clientWidth;
    //       console.log(height)//722
    //       console.log(width)//1295
    //   },
    //validate对整个表单进行校验的方法。若不传入回调函数，则会返回一个 promise
    loginForm() {
      this.$refs.loginFormref.validate(async valid => {
        //console.log(valid)
        if (!valid) {
          return
        } else {
          const { data: res } = await this.$http.post('login', this.LoginForm)
          //console.log(result)
          //console.log(res)
          if (res.meta.status !== 200) return this.$message.error('登录失败')
          this.$message.success('登录成功')
          //将登录成功之后的token,保存到客户端的sessionStorage中
          //项目中登录之外的其他api接口必须登录成功之后才能访问
          //token只应在当前打开期间生效，所以将token保存在sessionStorage中
          //通过编程式导航跳转到后台主页,路由地址是/home
          window.sessionStorage.setItem('token', res.data.token)
          this.$router.push('/homepage')
        }
      })
    },
    //点击重置按钮，重置登录表单
    resetloginForm() {
      //console.log(this)
      this.$refs.loginForm.resetFields()
    }
  }
}
</script>
<style lang="less" scoped>
.com-container {
  background-color: rgb(3, 14, 24);
  .login-box {
    width: 34%;
    height: 41.5%;
    background-color: aliceblue;
    position: absolute;
    border-radius: 3px;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    //头像区域
    .tou_box {
      width: 130px;
      height: 130px;
      border-radius: 50%;
      padding: 10px;
      left: 50%;
      position: absolute;
      box-shadow: 0px 0 10px rgb(121, 171, 238);
      transform: translate(-50%, -50%);
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        box-shadow: 0px 0 10px rgb(205, 121, 238);
      }
    }
    .tou_box:hover {
      transform: translate(-51%, -51%);
    }
    //表单区域
    .login_form {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;
      .btns {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
